//获取应用实例
var app = getApp();

// 初始化数据
var init = {

  // 数据
  data: {
    // 顶部幻灯片组件
    swiper: {

      // 图片列表
      banner: [],

      // 是否显示面板指示点
      indicatorDots: true,

      // 是否自动切换
      autoplay: true,

      // 自动切换时间间隔
      interval: 5000,

      // 滑动动画时长
      duration: 1000,

      // 是否采用衔接滑动	
      circular: true,

      // 当前图片下标
      index: 0
    },

    // 数据加载
    loading: true,
    show: false,
    shows: false,
    showid: "a",
    //文章tap
    articletap: 0,

  },
  onLoad: function (options) {
    this.getData();
  },
  //搜索
  show: function () {
    var that = this;
    that.setData({
      show: true
    })
  },
  hide: function () {
    var that = this;
    that.setData({
      show: false
    })
  },
  //搜索
  search: function (res) {
    var that = this;
    console.log(res);
    var keywords = res.detail.value || res.currentTarget.dataset.keywords;
    console.log(keywords);
    if (keywords != "" && keywords != undefined) {
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api,
          data: {
            app: 'article',
            act: "getSearchList",
            keywords: keywords,
            t_session: session
          },
          success: function (res) {
            var data = res.data;
            if (data.done && data.retval.articleList.length != 0) {
              wx.navigateTo({
                url: "/pages/articlescreen/articlescreen?keywords=" + keywords,
              })
            } else {
              wx.showToast({
                title: '暂无相关文章',
                icon: 'loading'
              })
            }
          }
        })
      })
    }
    that.setData({
      show: false
    })
  },

  // 页面加载
  onShow: function (options) {
    this.getData();
  },

  // 获取数据
  getData: function () {

    var that = this;

    // 请求前
    that.setData({
      loading: true,
    });
    app.getLoginSession(function (session) {
      wx.request({

        // 请求地址
        url: app.api,

        // 请求参数
        data: {
          app: 'article',
          act: 'index',
          t_session: session
        },

        // 请求成功
        success: function (res) {
          var data = JSON.parse(res.data.replace(/(^\s*)/g, ""));
          if (data.done && data.retval) { // 成功
            that.setData({

              // 关闭loading效果
              loading: false,

              // banner图片信息
              'swiper.banner': data.retval.banner,
              //搜索历史
              histroy: data.retval.histroy,
              //首页文章
              article: data.retval.article,
              //文章分类
              acategory: data.retval.acategory.reverse(),
              loading: false

            });

          } else { // 失败
            that.setData({
              errorTips: app.errorTips
            })
          }

        },

        // 请求失败
        fail: function () {
          that.setData({
            errorTips: app.errorTips
          })
        }
      })
    })

  },
  //筛选条件
  show2: function (e) {
    var that = this;
    console.log(e);
    var id = e.currentTarget.dataset.id;

    if (that.data.showid == "a") {
      that.setData({
        showid: id,
        shows: true,
        articletap: id,
        name: e.currentTarget.dataset.name
      })
    } else if (that.data.showid == id) {
      that.setData({
        shows: false,
        showid: "a"
      })
    } else {
      that.setData({
        shows: true,
        showid: id,
        articletap: id,
        name: e.currentTarget.dataset.name
      })
    }
  },
  // 标签
  items: function (event) {
    var that = this;
    var id = event.target.dataset.id;
    console.log(event);
    that.setData({
      id: id,
      tap1: event.target.dataset.name
    })
  },
  // 性别
  sex: function (event) {
    var that = this;
    var sex = event.target.dataset.sex;
    that.setData({
      sex: sex,
      tap2: event.target.dataset.name
    })
  },
  // 年龄
  age: function (event) {
    var that = this;
    var age = event.target.dataset.age;
    that.setData({
      age: age,
      tap3: event.target.dataset.name
    })
  },
  reset: function () {
    var that = this;
    that.setData({
      id: 'a',
      sex: '',
      age: ''
    })
    console.log("that.data.tap1" + that.data.tap1);
  },
  submit: function () {
    var that = this;
    if (that.data.tap1 == undefined) {
      wx.showToast({
        title: '请选择标签',
        icon: 'loading'
      })
      return false;
    }
    if (that.data.tap2 == undefined) {
      wx.showToast({
        title: '请选择性别',
        icon: 'loading'
      })
      return false;
    }
    if (that.data.tap3 == undefined) {
      wx.showToast({
        title: '请选择年龄',
        icon: 'loading'
      })
      return false;
    } else {
      var a = "?cate_name=" + that.data.tap1 + "&&sex=" + that.data.tap2 + "&&age=" + that.data.tap3 + "&&name=" + that.data.name;
      console.log(a);
      that.setData({
        shows: false
      })
      wx.navigateTo({
        url: "/pages/articlescreen/articlescreen" + a
      })
    }

  },
  //删除历史搜索
  drophistory: function () {
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'article',
          act: 'dropHistory',
          t_session: session
        },
        success: function (res) {
          var data = res.data;
          if (data.done && data.retval.code == 1) {
            wx.showToast({
              title: '删除成功',
              icon: 'success'
            })
            that.getData();
          } else {
            wx.showToast({
              title: '删除失败',
              icon: 'loading'
            })
          }
        }
      })
    })

  }


};

// 注册页面
Page(init);